package com.huluobo.a2204parkdemo.pagingAdapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.huluobo.a2204parkdemo.R
import com.huluobo.a2204parkdemo.databinding.ItemNewsBinding
import com.huluobo.a2204parkdemo.databinding.ItemNoticeBinding
import com.huluobo.base.bean.NoticeBeanItem

/**
 *  Created by LC on 2024/8/28.
 */
class NoticePagingAdapter : PagingDataAdapter<NoticeBeanItem, NoticePagingAdapter.ViewHolder>(diffUtilCallBack) {
    companion object {
        private val diffUtilCallBack = object : DiffUtil.ItemCallback<NoticeBeanItem>() {
            override fun areItemsTheSame(oldItem: NoticeBeanItem, newItem: NoticeBeanItem): Boolean {
                return oldItem.noticeId == newItem.noticeId
            }

            override fun areContentsTheSame(oldItem: NoticeBeanItem, newItem: NoticeBeanItem): Boolean {
                return oldItem == newItem
            }
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val binding: ItemNoticeBinding? = DataBindingUtil.bind(itemView)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding?.itemNoticeTitleTv?.text = getItem(position)?.noticeTitle
        holder.binding?.itemNoticeValueTv?.text = getItem(position)?.noticeValue
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_notice, parent, false)
        return ViewHolder(view)
    }
}